package com.westos.haohanbang.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

public class DelServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        //System.out.println(id);
        JSONObject json = new JSONObject();
        ServletContext application = getServletContext();
        Map haohans = (Map) application.getAttribute("haohans");

        System.out.println(haohans);
            if (haohans.containsKey(id)) {
                haohans.remove(id);
                json.put("msg", "该英雄已删除");
            } else {
                json.put("msg", "该英雄不存在");
            }
        System.out.println(haohans);
        json.put("haohans",haohans);
        response.getWriter().println(JSON.toJSONString(json, SerializerFeature.WriteSlashAsSpecial));

    }
}
